/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Administrator
 * @create 2019/3/16 0016
 */
package Service.Utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/3/16 0016
 */
public class TransferDateUtil {
    /**
     *
     *  根据输入的年月，以及第几周，获取第几周的周一周日日期
     */
    public static Map<String,String> getMonAndSunDay(int year,int month,int theWeek){

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1); //从0开始
        int day = 1;
        cal.set(Calendar.DAY_OF_MONTH, 1);    //从第一天开始找第一个星期一

        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(sdf.format(cal.getTime()));
        String firstDay = null;
        String lastDay  = null;
        if(theWeek>0 && theWeek < 6){
            while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
                cal.set(Calendar.DAY_OF_MONTH, ++day);
            }
            cal.add(Calendar.DATE, 7*(theWeek-1));
            Date getMonday =cal.getTime();
            cal.add(Calendar.DATE, 6);
            Date getSunday = cal.getTime();

            firstDay = sdf.format(getMonday);
            lastDay  = sdf.format(getSunday);

        }else{
            firstDay = sdf.format(cal.getTime());
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            lastDay = sdf.format(cal.getTime());
        }
//        System.out.println(firstDay);
//        System.out.println(lastDay);
        Map<String,String> dateStr = new HashMap<>();
        dateStr.put("firstDay", firstDay);
        dateStr.put("lastDay", lastDay);
        return dateStr;
    }
    /**
     * 功能描述: 获取上周周一周日时间
     */
    public static Map<String,String> getLastTimeInterval() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        String lastBeginDate = sdf.format(calendar1.getTime());
        String lastEndDate = sdf.format(calendar2.getTime());
        Map map = new HashMap();
        map.put("lastBeginDate", lastBeginDate);
        map.put("lastEndDate", lastEndDate);
        return map;
    }

}
